package com.quectel.util.common;

import java.util.Map;

/**
 * Map的增强器
 *
 * @author: caolu
 * @email: louis.cao@quectel.com
 * @date: 2023-03-31 10:43:00
 */
public class MapWrapper<K, V> {

    private Map<K, V> m;

    private MapWrapper() {
    }

    private MapWrapper(Map<K, V> m) {
        this.m = m;
    }

    /**
     * 创建增强对象
     *
     * @param m
     * @return
     */
    public static <K, V> MapWrapper<K, V> wrap(Map<K, V> m) {
        return new MapWrapper<>(m);
    }

    /**
     * key不区分大小写
     * 针对String的Key才能有这个方法
     *
     * @param key
     * @return
     */
    public V getValueByIgnoreStringKeyCase(String key) {
        if (key == null) {
            return null;
        }
        for (K mapKey : m.keySet()) {
            if (!(mapKey instanceof String)) {
                // key不是String
                continue;
            }
            String stringKey = (String) mapKey;
            if (stringKey.equalsIgnoreCase(key)) {
                return m.get(mapKey);
            }
        }
        return null;
    }
}
